//
//  ZHAlterView.m
//  test02
//
//  Created by lzh on 2020/10/15.
//  Copyright © 2020 NO. All rights reserved.
//

#import "ZHAlterView.h"
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
 blue:((float)(rgbValue & 0xFF))/255.0 \
alpha:1.0]
#define BUTTON_TAG 1000000

@interface ZHAlterView()

@property(nonatomic,strong)UIView * blackView;

@property(nonatomic,strong)UIView * alertview;

@property(nonatomic,strong)UIButton * leftBtn;

@property(nonatomic,strong)UIButton * rightBtn;

@property(nonatomic,strong)UIView * center_line;

@property(nonatomic,strong)UIView * line;

@end

@implementation ZHAlterView

-(id)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.blackView];
        
        [self addSubview:self.alertview];
    }
    
    return self;
}
+(ZHAlterView *)zh_alterView:(void(^)(ZHAlterView*))block{
    
    ZHAlterView * view = [[ZHAlterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    
    block(view);
    
    return view;
}

-(void)setLeftTitle:(NSString *)title{
    
    
    [self.leftBtn setTitle:title forState:UIControlStateNormal];

    [self.alertview  addSubview:self.leftBtn];

}
-(void)setContent:(NSString *)title{
    float alertW = 270;
       float alertH = 135;
       _alertview.frame = CGRectMake((SCREEN_W - alertW)/2.0f, (SCREEN_H - alertH)/2.0f, alertW, alertH);
    UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectZero];
          subTitle.text = title;
          subTitle.font = [UIFont boldSystemFontOfSize:18];
          subTitle.textAlignment = NSTextAlignmentCenter;
          subTitle.textColor = UIColorFromRGB(0x333333);
          subTitle.frame = CGRectMake(15, 0, alertW - 30, alertH - 50);
          [_alertview addSubview:subTitle];
    
}
-(void)setRightTitle:(NSString *)title{
    
    [self.rightBtn setTitle:title forState:UIControlStateNormal];

    [self.alertview  addSubview:self.rightBtn];

}

-(ZHAlterView *)show{
    
    UIView * keywindow = [UIApplication sharedApplication].keyWindow;
    
    [keywindow addSubview:self];
    return self;
}



-(UIView *)blackView{
    
    if (!_blackView) {
        
       _blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
              _blackView.backgroundColor = UIColorFromRGB(0x000000);
              _blackView.alpha = 0.7;
              UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(blackClick)];
              [_blackView addGestureRecognizer:tap];
    }
    
    return _blackView;
}
-(UIView *)alertview{
    
    if (!_alertview) {
        
        _alertview = [[UIView alloc] initWithFrame:CGRectZero];
        _alertview.layer.cornerRadius = (9);
        _alertview.clipsToBounds = YES;
        _alertview.backgroundColor = [UIColor whiteColor];
    }
    
    return _alertview;
}
-(UIButton *)leftBtn{
    
    if (!_leftBtn) {
        
        [self.alertview addSubview:self.line];
        float alertW = 270;
        float alertH = 135;
        _alertview.frame = CGRectMake((SCREEN_W - alertW)/2.0f, (SCREEN_H - alertH)/2.0f, alertW, alertH);
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.tag = BUTTON_TAG;
      _leftBtn.frame =  _rightBtn ?  CGRectMake(0, alertH - 50, alertW/2.0, 50) :  CGRectMake(0, alertH - 50, alertW, 50);
        _rightBtn.frame = _leftBtn ? CGRectMake(alertW/2.0+1, alertH - 50, alertW/2-1, 50) : CGRectMake(0, alertH - 50, alertW, 50) ;
        [_leftBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
               [_leftBtn.titleLabel setFont:[UIFont systemFontOfSize:(18)]];
         [_leftBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
    }
    return _leftBtn;
}

-(UIButton *)rightBtn{
    
    if (!_rightBtn) {
        
        [self.alertview addSubview:self.center_line];

        float alertW = 270;
        float alertH = 135;
        _alertview.frame = CGRectMake((SCREEN_W - alertW)/2.0f, (SCREEN_H - alertH)/2.0f, alertW, alertH);
        _rightBtn.tag = BUTTON_TAG;
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = _leftBtn ? CGRectMake(alertW/2.0+1, alertH - 50, alertW/2-1, 50) : CGRectMake(0, alertH - 50, alertW, 50) ;
        _leftBtn.frame =  _rightBtn ?  CGRectMake(0, alertH - 50, alertW/2.0, 50) :  CGRectMake(0, alertH - 50, alertW, 50);
        [_rightBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
               [_rightBtn.titleLabel setFont:[UIFont systemFontOfSize:(18)]];
         [_rightBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
    }
    return _rightBtn;
}

-(UIView *)center_line{
    
    float alertW = 270;
    float alertH = 135;
    if (!_center_line) {
        _center_line = [[UIView alloc]initWithFrame:CGRectMake(alertW/2.0, alertH-50, 1, 50)];
        _center_line.backgroundColor = UIColorFromRGB(0xf4f4f4);
             
    }
    
    return _center_line;
}

-(UIView *)line{
    
    if (!_line) {
        
        float alertW = 270;
        float alertH = 135;
        _line = [[UIView alloc] initWithFrame:CGRectMake(0, alertH - (50+1), alertW, 1)];
        _line.backgroundColor = UIColorFromRGB(0xf4f4f4);
       
    }
    
    return _line;
    
}

/**
@param need
  需要动画 为YES 不设置默认NO 没有动画
*/
-(void)setZh_animation:(BOOL)need{
    
    if (!need) {
        _zh_animation = need;
        return;
    }
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform";
    animation.duration = 0.6;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray * values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];
    [self.alertview.layer addAnimation:animation forKey:nil];
    
}
#pragma mark btn click
-(void)clickButton:(UIButton *)btn{
    
    [self cancleView];
    
    switch (btn.tag) {
        case BUTTON_TAG:
            self.shureComplete ? self.shureComplete() : nil ;
            break;
            
        default:
            
            self.cancelComplete ? self.cancelComplete() : nil;
            
            break;
    }
}
#pragma 隐藏弹窗
-(void)blackClick{
    
    [self cancleView];
}

#pragma mark -----关闭自定义弹窗
- (void)cancleView
{
    if (self==nil) {
        return;
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
    }];
    
    
}

@end
